import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/chart/line_chart1.dart';
import 'package:material_collection_app/widget/chart/ring_chart1.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

class DataAnalysis extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => MyState();
}

class TimeSeriesSales {
  final DateTime time;
  final int sales;

  TimeSeriesSales(this.time, this.sales);
}

class MyState extends State<DataAnalysis> {
  List counts = [0,0,0,0];
  Map data = {};
  List todayAnalysis = [0,0,0,0];
  List weekForFinish = [0,0,0,0,0,0,0];

  @override
  void initState() {
    super.initState();
    _getData();
  }

  // 获取数据
  _getData() {
    DioUtils.request("Statistics/getPersonalInfo",
      context: context,
      method: "GET",
      params: {},
      onSuccess: (res) {
        if(res['code'] == 1) {
          List chart1List = [];
          List chart2List = [];
          Map ta = res['data']['todayAnalysis'];
          Map wf = res['data']['weekForFinish'];

          counts[0] = res['data']['todayInc'];
          counts[1] = res['data']['todayFinish'];
          counts[2] = res['data']['todoAll'];
          counts[3] = res['data']['todayDiffYesterdayFinish'];

          chart1List.add(ta['todayInc']);
          chart1List.add(ta['todayFinish']);
          chart1List.add(ta['todayOvertime']);
          chart1List.add(ta['todoAll']);

          chart2List.add(wf['Monday']);
          chart2List.add(wf['Tuesday']);
          chart2List.add(wf['Wednesday']);
          chart2List.add(wf['Thursday']);
          chart2List.add(wf['Friday']);
          chart2List.add(wf['Saturday']);
          chart2List.add(wf['Sunday']);

          setState(() {
            data = res['data'];
            todayAnalysis = chart1List;
            weekForFinish = chart2List;
          });
        }else {
          Utils.showText(text: res['msg']);
        }
      },
      onError: (error) {
        // Toast.show(error);
      }
    );
  }
  
  Widget _buildData(title, number, src) {
    return Container(
      width: 250.w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Image.asset(src, width: 40.w),
              Container(
                margin: EdgeInsets.only(left: 10.w),
                child: Text(title,
                  style: TextStyle(
                    color: Utils.hexToColor('#B9BABE'),
                    fontSize: 14,
                    height: 1
                  )
                )
              )
            ]
          ),
          Container(
            margin: EdgeInsets.only(top: 22.w),
            child: Text(number.toString(), style: TextStyle(
              color: Utils.hexToColor('#333333'),
              fontSize: 24,
              fontWeight: FontWeight.bold
            ))
          )
        ]
      )
    );
  }

  Widget _buildLabel(title, color) {
    return Container(
      margin: EdgeInsets.only(left: 25.w),
      child: Row(
        children: <Widget>[
          Container(
            width: 36.w,
            height: 14.w,
            margin: EdgeInsets.only(right: 14.w),
            decoration: BoxDecoration(
              color: Utils.hexToColor(color),
              // #F1F1F1
              borderRadius: BorderRadius.circular(7.w)
            ),
          ),
          Text(title, style: TextStyle(
            color: Utils.hexToColor('#656565'),
            fontSize: 12
          ))
        ]
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return new Scaffold(
      appBar: new TopBar(
        title: '数据分析',
      ),
      body: SingleChildScrollView(
        child: Container(
          color: Utils.hexToColor('#F5F6FA'),
          child: Column(
            children: <Widget>[
              Stack(
                children: <Widget>[
                  Align(
                    child: Container(
                      height: 110.w,
                      color: Utils.hexToColor('#297CFF'),
                      padding: EdgeInsets.all(10),
                    ),
                  ),
                  Align(
                    child: Container(
                      width: 710.w,
                      height: 360.w,
                      margin: EdgeInsets.only(top: 20.w),
                      padding: EdgeInsets.fromLTRB(60.w, 40.w, 60.w, 0),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20.w)
                      ),
                      child: Wrap(
                        spacing: 90.w,
                        runSpacing: 50.w,
                        children: <Widget>[ 
                          _buildData('今日新增任务', counts[0], 'assets/d_add.png'),
                          _buildData('今日办结任务', counts[1], 'assets/d_comp.png'),
                          _buildData('待办任务', counts[2], 'assets/d_task.png'),
                          _buildData('同比昨日完成', counts[3], 'assets/d_day.png')
                        ]
                      )
                    )
                  )
                ]
              ),              
              Container(
                width: 710.w,
                height: 620.w,
                margin: EdgeInsets.only(top: 20.w),
                padding: EdgeInsets.fromLTRB(37.w, 28.w, 37.w, 0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w)
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: Text('今日工作任务分析图', style: TextStyle(
                        color: Utils.hexToColor('#333333'),
                        fontSize: 16,
                        fontWeight: FontWeight.bold
                      ))
                    ),
                    Container(
                      height: 400.w,
                      margin: EdgeInsets.only(top: 30.w),
                      child: RingChart(data: todayAnalysis),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 40.w),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          _buildLabel('待办', '#1FAFF7'),
                          _buildLabel('办结', '#1BC75D'),
                          _buildLabel('延期', '#7777FF'),
                          _buildLabel('新增', '#FFAF07'),
                        ]
                      )
                    )
                  ]
                )
              ),
              Container(
                width: 710.w,
                height: 540.w,
                margin: EdgeInsets.only(top: 20.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.w)
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.fromLTRB(37.w, 28.w, 37.w, 0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text('本周办结工单趋势图', style: TextStyle(
                            color: Utils.hexToColor('#333333'),
                            fontSize: 16,
                            fontWeight: FontWeight.bold
                          )),
                        ]
                      )
                    ),
                    Container(
                      height: 450.w,
                      padding: EdgeInsets.fromLTRB(17.w, 0.w, 5.w, 0),
                      child: LineChartSample(data: weekForFinish)
                    )
                  ]
                )
              ),
              Container(
                height: 40.w,
              )
            ]
          )
        )
      )
    );
  }
}